package com.itcast.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itcast.common.utils.RandomUtil;
import com.itcast.msm.service.MsmApiService;
import com.itcast.yygh.model.vo.msm.MsmVo;
import com.netflix.client.ClientException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.swing.text.html.FormSubmitEvent;
import java.rmi.ServerException;
import java.util.Map;

@Service
public class MsmApiServiceImpl implements MsmApiService {

    //发送短信实现
    @Override
    public boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            //String code = (String)msmVo.getParam().get("code");

            String code = RandomUtil.getFourBitRandom();
            return this.send(msmVo.getPhone(),code,null);
        }
        return false;
    }
    /**
     * 发送短信
     * @param phone
     * @param sms
     * @param map
     * @return
     */
    @Override
    public boolean send(String phone, String sms, Map<String, Object> map) {
        if(StringUtils.isEmpty(phone)) return false;

        /*DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(FormSubmitEvent.MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }*/
        return true;
    }
}
